<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class film_name_translation
{
	private $db;
	private $table_name = 'film_name_translation';
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		if(!isset($data['film_id'])||empty($data['film_id']))
		{
			return false;
		}
		$data_for_query['film_id'] = intval($data['film_id']);
		if(isset($data['en-US'])&&!empty($data['en-US']))
		{
			$data_for_query['en-US'] = trim($data['en-US']);
		}
		if(isset($data['yue-Hans'])&&!empty($data['yue-Hans']))
		{
			$data_for_query['yue-Hans'] = trim($data['yue-Hans']);
		}
		if(isset($data['yue-Hant'])&&!empty($data['yue-Hant']))
		{
			$data_for_query['yue-Hant'] = trim($data['yue-Hant']);
		}
		if(isset($data['zh-Hans-CN'])&&!empty($data['zh-Hans-CN']))
		{
			$data_for_query['zh-Hans-CN'] = trim($data['zh-Hans-CN']);
		}
		if(isset($data['zh-Hant-HK'])&&!empty($data['zh-Hant-HK']))
		{
			$data_for_query['zh-Hant-HK'] = trim($data['zh-Hant-HK']);
		}
		if(isset($data['zh-Hant-TW'])&&!empty($data['zh-Hant-TW']))
		{
			$data_for_query['zh-Hant-TW'] = trim($data['zh-Hant-TW']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$count = $this->db->exec($sql);
		if(empty($count))
		{
			//throw new Exception("INSERT INTO `".$this->table_name."` faild");
			throw new Exception("");
		}
		else
		{
			return $count;
		}
	}

	public function select($where = '1',$column='*')
	{
		$sql_1 = "SELECT ".$this->db->implodeToColumn($column)." FROM ".$this->table_name." WHERE ".$this->db->implodeToWhere($where);
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if($fetch_result==false)
		{
			//throw new Exception("faild");
			throw new Exception("");
		}
		else
		{
			return $fetch_result;
		}
	}

	public function getNameByFilmId($film_id)
	{
		$where = array('film_id'=>$film_id);
		$column = '*';
		$fetch_result = $this->select($where,$column);

		if(empty($fetch_result))
		{
			//throw new Exception("getIdAndName faild");
			throw new Exception("");
		}
		else
		{
			unset($fetch_result[0]['film_id']);
			return $fetch_result[0];
		}
	}
	
	public function update($where,$data)
	{
		if(isset($data['en-US']))
		{
			$data_for_query['en-US'] = trim($data['en-US']);
		}
		if(isset($data['yue-Hans']))
		{
			$data_for_query['yue-Hans'] = trim($data['yue-Hans']);
		}
		if(isset($data['yue-Hant']))
		{
			$data_for_query['yue-Hant'] = trim($data['yue-Hant']);
		}
		if(isset($data['zh-Hans-CN']))
		{
			$data_for_query['zh-Hans-CN'] = trim($data['zh-Hans-CN']);
		}
		if(isset($data['zh-Hant-HK']))
		{
			$data_for_query['zh-Hant-HK'] = trim($data['zh-Hant-HK']);
		}
		if(isset($data['zh-Hant-TW']))
		{
			$data_for_query['zh-Hant-TW'] = trim($data['zh-Hant-TW']);
		}
		if(!empty($data_for_query))
		{
			$sql = "UPDATE `".$this->table_name."` SET ".$this->db->implodeToUpdate($data_for_query)." WHERE ".$this->db->implodeToWhere($where);
			$count = $this->db->query($sql);
			return $count;
		}
		return true;
	}
	
	public function updateByFilmId($film_id,$data)
	{
		$where = array(
			'film_id' => $film_id,
		);
		return $this->update($where,$data);
	}
}

?>
